/*============================================================================
  File:      lrpmodel.h
 
  Summary:   Declaration of the pair-wise linear regression model and
			 and associated structures.
 
  Date:		 June 30, 2004
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.
 
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.
 
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
============================================================================*/
#pragma once
#include "DmhVector.h"
#include "DmhMemory.h"

// Individual regression parameters

struct LRPARAM
{
	double	_dblScore;		// Increase in marginal likelihood over no regressor
	double	_dblSlope;
	double	_dblIntercept;
	double	_dblSD;

	ULONG	_iAttributeRegressor;	// Allows sorting
	ULONG	_iAttributeTarget;
};

DEFINEV(LRPARAM);
DEFINEV(VLRPARAM);

DEFINEV(DBL);
DEFINEV(VDBL);

class LRSSTATREADER;

// Pair-wise linear regression model

class LRPMODEL : public DMHALLOC
{
  public:

	LRPMODEL() : _vvlrparamView(*this), 
				 _vvlrparamPredict(*this), 
				 _viAttributeOutput(*this),
				 _vdblSampleSD(*this),
				 _vdblSampleMean(*this),
				 _vdblPostSD(*this),
				 _vdblMin(*this),
				 _vdblMax(*this)
	{
		_cAttribute = 0;
		_cCase		= 0;
	}

	virtual HRESULT	PopulateModel(LRSSTATREADER& lrsstatreader, DBL	dblMinDepScore);

	virtual HRESULT	ExtractPosterior(const VDBL&	vdblValueDense,		// [iAttribute]
								     ULONG			iAttributeTarget,
									 DBL&			dblPostMean,
									 DBL&			dblPostSD);

	ULONG		_cAttribute;
	ULONG		_cCase;

 	// Parameters for continuous part: this is a bit confusing because we're using
	// naive-Bayes inference to do Predict() calls. For every OUTPUT variable O specified
	// by the user, we want to keep, for every (significant) input variable I the 
	// regression:
	//
	//	O = m I + b
	//
	// to display to the user when browsing. For inference, however, we need to keep
	// the models
	//
	// I = m O + b
	// 
	// instead. We store "viewing" parameters "by output", and the "predict" parameters
	// "by input". In particular:
    	
	VVLRPARAM	_vvlrparamView;		// [iAttributeOutput]
									// All elements of _vvlrparamView[i] have i as the
									// target attribute in the regression

	VVLRPARAM	_vvlrparamPredict;	// [iAttributeOutput]
									// All elements of _vvlrparamPredict[i] have i as
									// the regressor attribute in the regression

	VINT		_viAttributeOutput; // Attribute indices corresponding to outputs
    
	// Sample mean and SD for each attribute

	VDBL			_vdblSampleSD;		// [iAttribute]
	VDBL			_vdblSampleMean;	// [iAttribute]

	// Min and max value for each attribute

	VDBL			_vdblMin;		// [iAttribute]
	VDBL			_vdblMax;		// [iAttribute]

	// Posterior SD for each attribute

	VDBL			_vdblPostSD;	// [iAttribute]
};